package com.gxar.quick.ar.server.base.async.works.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.quick.ar.server.base.async.works.event.WorksPreviewDeleteEvent;
import com.gxar.quick.ar.server.base.common.enums.ComboConfigItemEnum;
import com.gxar.quick.ar.server.base.common.enums.DeletedEnum;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.works.dto.WorksContentDTO;
import com.gxar.quick.ar.server.base.reference.FileReference;
import com.gxar.quick.ar.server.db.model.WorksPreviewEntity;
import com.gxar.quick.ar.server.db.repository.WorksPreviewRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/24 15:33
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WorksPreviewDeleteListener implements ApplicationListener<WorksPreviewDeleteEvent> {

    private final FileReference fileReference;
    private final WorksPreviewRepository worksPreviewRepository;

    @Override
    @Async
    public void onApplicationEvent(WorksPreviewDeleteEvent event) {
        log.info("线程[{}]-执行删除预览作品", Thread.currentThread().getName());
        List<WorksPreviewEntity> worksPreviewEntityList = event.getWorksPreviewEntityList();
        if (CollectionUtils.isEmpty(worksPreviewEntityList)) {
            return;
        }
        Date date = new Date();
        for (WorksPreviewEntity worksPreviewEntity : worksPreviewEntityList) {
            worksPreviewEntity.setDeleted(DeletedEnum.TRUE.getCode());
            worksPreviewEntity.setUpdatedAt(date);
        }
        worksPreviewRepository.saveAll(worksPreviewEntityList);
        // 批量解析配置删除
//        List<String> fileUrl = new ArrayList<>();
//        for (WorksPreviewEntity entity : worksPreviewEntityList) {
//            try {
//                JSONObject identifyObject = JSONObject.parseObject(entity.getIdentify());
//                if (identifyObject != null) {
//                    fileUrl.add(identifyObject.getString("linkUrl"));
//                }
//            } catch (Exception e) {
//                // 解析识别图异常
//            }
//            try {
//                List<WorksContentDTO> worksContentDTOList = JSON.parseArray(entity.getContent(), WorksContentDTO.class);
//                List<String> contentLinkUrlList = Optional.ofNullable(worksContentDTOList).orElse(Collections.emptyList()).stream().map(WorksContentDTO::getLinkUrl).collect(Collectors.toList());
//                if (CollectionUtils.isNotEmpty(contentLinkUrlList)) {
//                    fileUrl.addAll(contentLinkUrlList);
//                }
//            } catch (Exception e) {
//                // 解析内容异常
//            }
//        }
//        // 文件地址去重复
//        List<String> distinctLinkUrlList = fileUrl.stream().distinct().collect(Collectors.toList());
//        if (CollectionUtils.isNotEmpty(distinctLinkUrlList)) {
//            List<String> deletedLinkUrlList = new ArrayList<>();
//            Set<String> extUrlSet = event.getExtUrlSet();
//            for (String linkUrl : distinctLinkUrlList) {
//                if (!extUrlSet.contains(linkUrl)) {
//                    deletedLinkUrlList.add(linkUrl);
//                }
//            }
//            if (CollectionUtils.isNotEmpty(deletedLinkUrlList)) {
//                fileReference.batchFileDelete(event.getUserId(), deletedLinkUrlList);
//            }
//        }
    }

}
